package com.fengze.business.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fengze.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fengze.common.annotation.Log;
import com.fengze.common.core.controller.BaseController;
import com.fengze.common.core.domain.AjaxResult;
import com.fengze.common.enums.BusinessType;
import com.fengze.business.domain.InvoiceInfo;
import com.fengze.business.service.IInvoiceInfoService;
import com.fengze.common.utils.poi.ExcelUtil;
import com.fengze.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 发票信息Controller
 * 
 * @author lug
 * @date 2024-02-28
 */
@RestController
@RequestMapping("/invoice/info")
public class InvoiceInfoController extends BaseController
{
    @Autowired
    private IInvoiceInfoService invoiceInfoService;

    /**
     * 查询发票信息列表
     */
    @PreAuthorize("@ss.hasPermi('invoice:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(InvoiceInfo invoiceInfo)
    {
        startPage();
        List<InvoiceInfo> list = invoiceInfoService.selectInvoiceInfoList(invoiceInfo);
        return getDataTable(list);
    }

    /**
     * 导出发票信息列表
     */
    @PreAuthorize("@ss.hasPermi('invoice:info:export')")
    @Log(title = "发票信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvoiceInfo invoiceInfo)
    {
        List<InvoiceInfo> list = invoiceInfoService.selectInvoiceInfoList(invoiceInfo);
        ExcelUtil<InvoiceInfo> util = new ExcelUtil<InvoiceInfo>(InvoiceInfo.class);
        util.exportExcel(response, list, "发票信息数据");
    }

    /**
     * 获取发票信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('invoice:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(invoiceInfoService.selectInvoiceInfoById(id));
    }

    /**
     * 新增发票信息
     */
    @PreAuthorize("@ss.hasPermi('invoice:info:add')")
    @Log(title = "发票信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(InvoiceInfo invoiceInfo, MultipartFile [] files)
    {
        return toAjax(invoiceInfoService.insertInvoiceInfoWithFiles(invoiceInfo,files));
    }

    /**
     * 修改发票信息
     */
    @PreAuthorize("@ss.hasPermi('invoice:info:edit')")
    @Log(title = "发票信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(InvoiceInfo invoiceInfo,MultipartFile [] files)
    {
        return toAjax(invoiceInfoService.updateInvoiceInfoWithFiles(invoiceInfo,files));
    }

    /**
     * 删除发票信息
     */
    @PreAuthorize("@ss.hasPermi('invoice:info:remove')")
    @Log(title = "发票信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(invoiceInfoService.deleteInvoiceInfoByIds(ids));
    }

    /**
     * 修改发票信息
     */
    @PreAuthorize("@ss.hasPermi('invoice:info:examine')")
    @Log(title = "发票信息审核", businessType = BusinessType.UPDATE)
    @PutMapping("/examine/{id}")
    public AjaxResult examine(@PathVariable("id") Long id)
    {
        Long userId = SecurityUtils.getUserId();
        Date now = Calendar.getInstance().getTime();
        int count = invoiceInfoService.examineInfo(id,userId,now,1);
        if(count > 0){
            return AjaxResult.success("审核成功");
        }
        return AjaxResult.error("审核失败");
    }


    /**
     * 开票接口
     * @param invoiceInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('invoice:info:open')")
    @Log(title = "发票信息审核", businessType = BusinessType.UPDATE)
    @PutMapping("/open")
    public AjaxResult open(InvoiceInfo invoiceInfo)
    {
        Long userId = SecurityUtils.getUserId();

//        Date now = Calendar.getInstance().getTime();
//        invoiceInfo.setInvoiceTime(now);
        invoiceInfo.setInvoiceUserId(userId);
        int count = invoiceInfoService.open(invoiceInfo);
        if(count > 0){
            return AjaxResult.success("开票成功");
        }
        return AjaxResult.error("开票失败");
    }

}
